/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgUtil.pkgFrameworkClipboard;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 *
 * @author DICSYS
 */
public class UtilClipboard {

    public final static String ID_COPY = "COPY";
    public final static String ID_PASTE = "PASTE";

    public static Clipboard getSystemClipboard() {
        return Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public static KeyStroke getKeyStrokeForCopy(boolean onKeyRelease) {
        return KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, onKeyRelease);
    }

    public static KeyStroke getKeyStrokeForPaste(boolean onKeyRelease) {
        return KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, onKeyRelease);
    }

    public static void registerListenerForCopyPaste(JComponent jComponent, ActionListener listener) {
        KeyStroke copy = getKeyStrokeForCopy(false);
        // Identifying the copy KeyStroke user can modify this
        // to copy on some other Key combination.
        KeyStroke paste = getKeyStrokeForPaste(false);
        // Identifying the Paste KeyStroke user can modify this
        //to copy on some other Key combination.
        jComponent.registerKeyboardAction(listener, ID_COPY, copy, JComponent.WHEN_FOCUSED);
        jComponent.registerKeyboardAction(listener, ID_PASTE, paste, JComponent.WHEN_FOCUSED);
    }
    
    public static void setContentsClipboard(StringSelection value){
        getSystemClipboard().setContents(value, value);
    }
    
    public static String getContentsFromClipboard(Object requestor) throws UnsupportedFlavorException, IOException{
        return (String)getSystemClipboard().getContents(requestor).getTransferData(DataFlavor.stringFlavor);
    }
}
